package de.softwareforge.published.util;

/*
 * Copyright (C) 2007-2008 Henning P. Schmiedehausen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Container for all information related to a given publishing level.
 *
 * @author <a href="mailto:henning@schmiedehausen.org">Henning P. Schmiedehausen</a>
 * @version $Id:PublishInformation.java 357 2007-08-13 20:57:20Z hps@intermeta.de $
 * @api.level unpublished
 */
public class PublishInformation
        implements Comparable
{

    /** "Unpublished" Definition. */
    public static final PublishInformation UNPUBLISHED =
        new PublishInformation("unpublished", null, 0, "background-color:#ff4444; padding: 5px;");

    /** "Published Unstable" Definition. */
    public static final PublishInformation UNSTABLE =
        new PublishInformation("published", "unstable", 1, "background-color:#ff8800; padding: 5px;");

    /** "Published Revision" Definition. */
    public static final PublishInformation REVISION =
        new PublishInformation("published", "revision", 2, "background-color:#dbfd00; padding: 5px;");

    /** "Published Minor" Definition. */
    public static final PublishInformation MINOR =
        new PublishInformation("published", "minor", 3, "background-color:#aad000; padding: 5px;");

    /** "Published Major" Definition. */
    public static final PublishInformation MAJOR =
        new PublishInformation("published", "major", 4, "background-color:#00d000; padding: 5px;");

    /** "Published Stable" Definition. */
    public static final PublishInformation PUBLISHED =
        new PublishInformation("published", "stable", 5, "background-color:#00ff00; padding: 5px;");

    private final String published;

    private final String level;

    private final String style;

    private final int prio;

    /**
     * Creates a new PublishInformation object.
     *
     * @param published The marker for the published state.
     * @param level The level inside the published state.
     * @param prio A prio code. Is ordered from "unpublished" to "published stable"
     * @param style Style information for displaying this publishing level.
     */
    public PublishInformation(final String published, final String level, final int prio, final String style)
    {
        this.published = published;
        this.level = level;
        this.style = style;
        this.prio = prio;
    }

    /**
     * Return the published state.
     *
     * @return The published state.
     */
    public String getPublished()
    {
        return published;
    }

    /**
     * Return the published level.
     *
     * @return The published level.
     */
    public String getLevel()
    {
        return level;
    }

    /**
     * Return the display style.
     *
     * @return The display style.
     */
    public String getStyle()
    {
        return style;
    }

    /**
     * Return the publishing priority.
     *
     * @return The publishing priority.
     */
    public int getPrio()
    {
        return prio;
    }

    /**
     * Return an unique lookup key, e.g. for locale.
     *
     * @return A lookup key.
     */
    public String getKey()
    {
        return getKey("_");
    }

    /**
     * Returns the value of the key property.
     *
     * @param separator A separator string between the elements of the key.
     *
     * @return A String representing this object with the given separator used.
     */
    protected String getKey(final String separator)
    {
        StringBuffer sb = new StringBuffer(getPublished());

        if (getLevel() != null)
        {
            sb.append(separator).append(getLevel());
        }

        return sb.toString();
    }

    /**
     * @see Comparable#compareTo(Object)
     */
    public int compareTo(final Object o)
    {
        PublishInformation publishInformation = (PublishInformation) o;

        if (this.getPrio() < publishInformation.getPrio())
        {
            return -1;
        }
        else if (this.getPrio() > publishInformation.getPrio())
        {
            return 1;
        }

        return 0;
    }

    /**
     * @see Object#equals(Object)
     */
    @Override
    public boolean equals(final Object o)
    {

        if (o == null)
        {
            return false;
        }

        if (o == this)
        {
            return true;
        }

        if (!(o instanceof PublishInformation))
        {
            return false;
        }

        return ((PublishInformation) o).getPrio() == getPrio();
    }

    /**
     * @see Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        return super.hashCode();
    }

    /**
     * @see Object#toString()
     */
    @Override
    public String toString()
    {
        return getKey(" ");
    }
}
